package com.clouddot.baipai.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;

import com.clouddot.baipai.ui.util.ToastUtil;
import com.clouddot.baipai.ui.util.asy.AsyCallbackListener;
import com.clouddot.baipai.ui.util.asy.AsyExecuteListener;
import com.clouddot.baipai.ui.util.asy.AsyHelper;

import static java.lang.Thread.sleep;


public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView();// 设置布局
        initControls();// 初始化控件
        setListener();// 设置控件监听
        loadParams();// 加载传递的参数


    }

    protected void setContentView() {}

    protected void initControls() {

    }

    protected void setListener() {

    }

    protected void loadParams() {

    }

    public void showLoading() {

    }

    public void closeLoading() {

    }

    public void runThreadAction() {
        AsyHelper.create(new AsyExecuteListener() {
                             public Object run() throws Exception {
                                 return runThreadActionExceute();
                             }

                         },

                new AsyCallbackListener() {
                    public void run(Object result) {
                        runThreadActionSuccess(result);
                    }

                    public void error(Exception ex) {
                        runThreadActionError(ex);
                    }
                });
    }

    protected Object runThreadActionExceute() {
        showLoading();
        return null;
    }

    protected void runThreadActionSuccess(Object result) {
        closeLoading();
    }

    protected void runThreadActionError(Exception ex) {
        closeLoading();
        ToastUtil.showToast(BaseActivity.this, ex.getMessage(), 3000);
    }

    public void runThreadAction(String actionName) {
        final String name = actionName;
        AsyHelper.create(new AsyExecuteListener() {
                             public Object run() throws Exception {
                                 return runThreadActionExceute(name);
                             }

                             ;
                         },

                new AsyCallbackListener() {
                    public void run(Object result) {
                        runThreadActionSuccess(name, result);
                    }


                    public void error(Exception ex) {
                        runThreadActionError(name, ex);
                    }
                });
    }

    protected Object runThreadActionExceute(String actionName) {
        showLoading();
        return null;
    }

    protected void runThreadActionSuccess(String actionName, Object result) {
        closeLoading();
    }

    protected void runThreadActionError(String actionName, Exception ex) {
        closeLoading();
        ToastUtil.showToast(BaseActivity.this, ex.getMessage(), 3000);
    }

    protected <T> T find(int resourceId) {
        return (T) findViewById(resourceId);
    }





}
